package com.itheima.mq.rabbitmq.configuration;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * 测试Direct类型的交换机
 */
//@Component
@Configuration
public class RabbitmqDirectExchangeConfiguration {

    // 声明交换机
    @Bean(name = "direct.exchange")
    public Exchange directExchange() {
        return ExchangeBuilder.directExchange("direct.exchange").durable(true).build() ;
    }

    // 声明队列
    @Bean(name = "direct.queue_01")
    public Queue commonQueue01() {
        return QueueBuilder.durable("direct.queue_01").build() ;
    }

    // 声明队列
    @Bean(name = "direct.queue_02")
    public Queue commonQueue02() {
        return QueueBuilder.durable("direct.queue_02").build() ;
    }

    //声明一个超时的队列
    @Bean(name = "direct.queue_03")
    public Queue commonQueue03() {
        QueueBuilder durable = QueueBuilder.durable("direct.queue_03");
        durable.ttl(15000);
        //设置该队列的死信属性
        //当该消息队列中的消息超时后会将消息发送到dlx.exchange交换机中的路由键为dlx的队列中去
        durable.deadLetterExchange("dlx.exchange");
        durable.deadLetterRoutingKey("dlx");

        return durable.build();
    }

    // 完成队列和交换机的绑定
    @Bean
    public Binding directQueueBindToFanoutQueue01(@Qualifier(value = "direct.exchange") Exchange exchange , @Qualifier(value = "direct.queue_01") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("create").noargs() ;
    }

    // 完成队列和交换机的绑定
    @Bean
    public Binding directQueueBindToFanoutQueue02(@Qualifier(value = "direct.exchange") Exchange exchange , @Qualifier(value = "direct.queue_02") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("delete").noargs() ;
    }

    //将超时队列与交换机绑定
    @Bean
    public Binding directQueueBindToFanoutQueue03(@Qualifier(value = "direct.exchange") Exchange exchange , @Qualifier(value = "direct.queue_03") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs() ;
    }

}
